/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Accounts;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;
import util.Md5Generator;

/**
 *
 * @author NDH
 */
public class AccountDAO extends AbstractDAO {

    /**
     * @param mail an email to retrieve its correlative account
     * @return Accounts
     * @see Account's information
     */
    public Accounts readByEmail(String mail) {
        try {
            query = session.createQuery("from Accounts where upper(email) = :mail");
            query.setString("mail", mail.toUpperCase());
            return (Accounts) query.list().get(0);
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * @param user username to log in the system
     * @param pass password to log in the system
     * @return Accounts
     */
    public Accounts login(String user, String pass) {
        session = HibernateUtil.getSessionFactory().openSession();
        String hashed = Md5Generator.getMd5(pass);
        try {
            query = session.createQuery("from Accounts where username = :user and password = :pass");
            query.setParameter("user", user);
            query.setParameter("pass", hashed);
            return (Accounts) query.list().get(0);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param Accounts an string of account's fields entered to create new one
     * @return int
     * @see Accounts
     */
    public int create(Accounts lg) {
        try {
            session = HibernateUtil.getSessionFactory().getCurrentSession();
            tx = session.beginTransaction();
            query = session.createQuery("from Accounts where userName = :username");
            query.setParameter("username", lg.getUserName());
            if (query.list().size() > 0) {
                return 0;
            }
            session.save(lg);
            tx.commit();
            return 1;
        } catch (Exception ex) {
            tx.rollback();
            return -1;
        }
    }

    /**
     * @param Accounts an string of account's fields entered to update edited
     * info
     * @return int
     * @see Accounts
     */
    public int update(Accounts lg) {
        try {

//            Session session = null;s
            session = HibernateUtil.getSessionFactory().openSession();
            lg = (Accounts) session.merge(lg);
            tx = session.beginTransaction();
            session.save(lg);
            tx.commit();
            return 1;
        } catch (Exception e) {
            tx.rollback();
            return -1;
        } finally {
        }
    }

    /**
     * @param username an username to read its corrective account
     * @return an account and relating info
     * @see Accounts
     */
    public Accounts readByUsername(String username) {
        try {
            query = session.createQuery("from Accounts where userName = :username");
            query.setParameter("username", username);
            return (Accounts) query.list().get(0);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param Accounts an account to check
     * @param roleID a roleID to check whether it is staff or student
     * @return true / false
     */
    public boolean checkUserHaveRole(Accounts acc, int roleID) {
        query = session.createQuery("from Accounts where userName = :username and roleId = :roleId");
        query.setParameter("username", acc.getUserName());
        query.setParameter("roleId", roleID);
        try {
            if (query.list().size() > 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }
}
